/*
 	Script: GearsORM.Introspection.js
 	        Contains the GearsORM.Introspection utility class,
 	        which contain also a model for sqlite_master table
 	
 	License:
 	        MIT-style license.
 */

/*
  Class: GearsORM.Introspection
  	a utility class to do database introspection.
 */
GearsORM.Introspection =
{
	/*
	 Property: GearsORM.Introspection.sqliteMasterModel
	 	a model to work with sqlite_master(provide information on the schema) table
		sqlite_master definition from sqlite site:
		 CREATE TABLE sqlite_master (
		  type TEXT,
		  name TEXT,
		  tbl_name TEXT,
		  rootpage INTEGER,
		  sql TEXT
		);
	 */
	sqliteMasterModel:new GearsORM.Model({
		name:"sqlite_master",
		fields:
		{
			type:new GearsORM.Fields.String(),
			name:new GearsORM.Fields.String(),
			tbl_name:new GearsORM.Fields.String(),
			rootpage:new GearsORM.Fields.Integer(),
			sql:new GearsORM.Fields.String()
		}
	}),
	/*
	 Function: doesTableExist
	 	get the names of the tables to checked if they exist return true if *all* of them exist
	 	else return false
	 	
	 */
	doesTableExist:function()
	{
		var argsLength = arguments.length;
		var qmarks = [];
		//used instead of ["table"].concat(arguments) since it doesn`t work as expected
		var vals = ["table"];
		for(var i=0;i<argsLength;i++)
		{
			qmarks.push("?");
			vals.push(arguments[i]);	
		}
		var query = ["sqlite_master.type =? AND sqlite_master.name IN (",qmarks.join(","),")"].join("");
		
		return GearsORM.Introspection.sqliteMasterModel
		.count(query,vals) == argsLength;
	}
};